window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.fillHead();
		U.fillFooter();
		var user = U.checkLogin();
		var id = U.getParam('uid');
		var groupId = '';
		var projectId = '';
		var principalId = '';
		initPage();

		function initPage() { //初始化页面
			if(user.state == 0) {
				$("body").empty();
				M.toast(false, "请先登录!", function() {
					U.loadLogin(true, false);
				});
			} else {
				$("body>div").not('.h_apply').show();
			}
			U.loadOpt(false);
			initEvent();
		}

		function initEvent() {
			$('.add_img').click(function() {
				$('.task_detail').show();
				$('.save').show();
			});
			U.ajax('/groupDetail/detail/' + id, {}, function(json) { //团队人员详情
				if(json.success) {
					var result = json.models.groupDetailVO;
					var sex = result.sex == 1 ? '女' : '男';
					$('.headImg').attr('src', result.headImage);
					$('.code').text(U.formPhone(result.cardCode));
					$('.position').text(result.position);
					$('.name').text(result.name);
					$('.weixin').text(result.weixin);
					$('.phone').text(U.formPhone(result.mobile));
					$('.address').text(result.address);
					$('.city').text(result.city);
					$('.area').text(result.helpArea);
					$('.idea').text(result.requirement);
					$('.remark').text(result.remark);
					$('.sex_span').text(sex);
					$('.csxz').text(result.address);
					$('.szcs').text(result.city);
				}
			});
			U.ajaxget('/mission/getByUserId/' + 5, {}, function(json) { //获取任务列表
				if(json.success) {
					$('.list').html('');
					var result = json.models.missionList;
					$(result).each(function() {
						groupId = this.groupId;
						projectId = this.projectId;
						principalId = this.principalId;
						if(this.state == 0) {
							this.state = '完成'
							state = 0;
						} else {
							this.state = '已完成'
							state = 1;
						}
						$('.list').append('<ol id=' + this.id + '><li>' + this.title + '</li><li>' + U.getMyDate(this.beginTime) + '</li><li>' + U.getMyDate(this.endTime) + '</li><li><span class="finish">'+this.state+'</span><span class="delete">删除</span></li></ol>');
					});
					delete_task();
					finish_task();
				} else {
					alert(json.errorMsg);
				}
			});
		}

		function delete_task() { //删除任务
			$('.delete').click(function() {
				var temp = $(this).parent().parent();
				var tid = temp.attr('id');
				U.ajaxget('/mission/del/' + tid, {}, function(json) {
					if(json.success) {
						alert('删除成功!');
						temp.remove();
					} else {
						alert(json.errorMsg);
					}

				});
			});
		}

		function finish_task() {//更改未完成的任务为已完成
			$('.finish').click(function() {
				var temp = $(this).parent().parent();
				var $this=$(this);
				if($this.text().trim()=='已完成'){
					alert('改任务已完成,不能再修改状态!');
					return;
				}
				var tid = temp.attr('id');
				U.ajaxget('/mission/update/' + tid + '/' + 1, {}, function(json) {
					if(json.success) {
						M.toast(true, '状态更改成功！', function() {
						$this.text('完成');
						});
					} else {
						alert(json.errorMsg);
					}
				});
			});
		}
		$('.save').click(function() { //添加任务
			var missionVO = {
				groupId: groupId,
				projectId: projectId,
				principalId: principalId,
				userId: id,
				title: $('textarea').val(),
				beginTime: U.translate_time($('.start').val(), "-", "/"),
				endTime: U.translate_time($('.end').val(), "-", "/")
			}
			if($('.start').val().trim() == '' || $('.end').val().trim() == '' || $('textarea').val().trim() == '') {
				M.toast(false, '请填完整任务信息！');
				return;
			}
			U.ajax('/mission/add', missionVO, function(json) {
				if(json.success) {
					M.toast(true, '添加成功！', function() {
						location.href = '';
					})
				} else {
					alert(json.errorMsg);
				}
			});

		});
	});
}